//
//  OrderDM.m
//  WGFGlobalHouses
//
//  Created by welldone on 15/5/18.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "OrderDM.h"
#import "API.h"
#import "OrderData.h"
#import "StringTool.h"
#import "Tools.h"
#import "OrderDetailData.h"
#import "NearbyCellData.h"

#import "UserModal.h"





@interface OrderDM ()
@property (nonatomic,assign)int pageIndex;

@end


@implementation OrderDM
@synthesize pageIndex;



// 0  1  2   全部 已出售  交易中
-(ResulteMod*)refreshData:(Order_State)flag  WithCache:(BOOL)caChe{

    pageIndex = 1;
    NSString *url = [NSString stringWithFormat:@"%@%ld/%ld/%d/%@",[API urlMake:OrderList],(long)[UserModal shareUserModel].userId,(long)flag,pageIndex,@"8"];
    
    ResulteMod *mod;
    
    if (caChe) {
        mod = [Connect doGetWithCaChe:url];
    }else{
        mod = [Connect doGetWithOutCaChe:url];
    }
    
    //返回格式不同 需要单独处理
    NSString *code = [NSString stringWithFormat:@"%@",mod.resultDic[@"code"]];
    if ([StringTool isBlankString:code]) {
        mod.resultCode = @"";
    }else{
        mod.resultCode = code;
    }
    mod.resultTip = mod.resultDic[@"message"];
    
    if (![mod.resultCode isEqualToString:@"20003"]){
        mod.resltState = ResultFail;
        return mod;
    }
    
    NSDictionary *resDic = mod.resultDic;
    NSDictionary *abstractResponseDic = resDic[@"orderListDto"];
    NSArray *mainArray =abstractResponseDic[@"orderDtos"];
    
    NSMutableArray *array = [[NSMutableArray alloc]init];
    for (NSDictionary *dic in mainArray) {
        
        OrderData *data = [[OrderData alloc]init];
        
        long long ddddd = [dic[@"createTime"] longLongValue];
        [data setTime:[Tools llmilToDay:ddddd]];
        [data setTitle:dic[@"housingName"]];
        [data setOrderCode:dic[@"orderCode"]];
        [data setImgUrl:[API imgUrlMake:dic[@"thumb"]]];
        [data setDol:dic[@"amountUsd"]];
        [data setRbm:dic[@"amountCny"]];
        
        NSString *country = dic[@"countryName"];
        NSString *city = dic[@"cityName"];
        NSString *zhou = dic[@"stateName"];
        
        NSString *str = [NSString stringWithFormat:@"%@-%@-%@"
                         ,country,city,zhou];
        [data setSubTitle:str];
        
        
        NSInteger state = 0;
        NSObject *stateObj = dic[@"masterState"];
        if (![stateObj isKindOfClass:[NSNull class]]) {
            state = [dic[@"masterState"] integerValue];
        }

        if(state == 1010){
            [data setState:Progress1];
        }else if(state == 1030){
            [data setState:Progress2];
        }else if(state==1050){
            [data setState:Progress3];
        }else if(state==2010){
            [data setState:Progress4];
        }else if (state == 3010){
            [data setState:Progress5];
        }else if (state == 3020){
            [data setState:Progress6];
        }else if(state == 9000){
            [data setState:Progress7];
        }else if(state == 9020){
            [data setState:Progress8];
        }else if(state == 9030){
            [data setState:Progress9];
        }else{
            [data setState:Progress1];
        }
        data.lockEndTime = dic[@"lockEndTime"];
        data.masterState = [dic[@"masterState"]integerValue];
    
        [array addObject:data];
    }
    
    
    if (array.count>0) {
        pageIndex++;
    }

    mod.dataMod = array;
    return mod;
}


-(ResulteMod*)loadMoreData:(Order_State)flag{

    NSString *url = [NSString stringWithFormat:@"%@%ld/%ld/%d/%@",[API urlMake:OrderList],(long)[UserModal shareUserModel].userId,(long)flag,pageIndex,@"8"];
    
    
    ResulteMod *mod = [Connect doGetWithCaChe:url];
    
    //返回格式不同 需要单独处理
    NSString *code = [NSString stringWithFormat:@"%@",mod.resultDic[@"code"]];
    if ([StringTool isBlankString:code]) {
        mod.resultCode = @"";
    }else{
        mod.resultCode = code;
    }
    mod.resultTip = mod.resultDic[@"message"];
    
    if (![mod.resultCode isEqualToString:@"20003"]){
        mod.resltState = ResultFail;
        return mod;
    }
    
    
    NSDictionary *resDic = mod.resultDic;
    NSDictionary *abstractResponseDic = resDic[@"orderListDto"];
    NSArray *mainArray =abstractResponseDic[@"orderDtos"];
    
    NSMutableArray *array = [[NSMutableArray alloc]init];
    for (NSDictionary *dic in mainArray) {
        
        OrderData *data = [[OrderData alloc]init];
        

        long long ddddd = [dic[@"createTime"] longLongValue];
        [data setTime:[Tools llmilToDay:ddddd]];
        
        [data setTitle:dic[@"housingName"]];
        [data setOrderCode:dic[@"orderCode"]];
        [data setImgUrl:[API imgUrlMake:dic[@"thumb"]]];
        [data setDol:dic[@"amountUsd"]];
        [data setRbm:dic[@"amountCny"]];
        
        NSString *country = dic[@"countryName"];
        NSString *city = dic[@"cityName"];
        NSString *zhou = dic[@"stateName"];
        
        NSString *str = [NSString stringWithFormat:@"%@-%@-%@"
                         ,country,city,zhou];
        [data setSubTitle:str];
        
        
        NSInteger state = 0;
        NSObject *stateObj = dic[@"masterState"];
        if (![stateObj isKindOfClass:[NSNull class]]) {
            state = [dic[@"masterState"] integerValue];
        }
        
        if(state == 1010){
            [data setState:Progress1];
        }else if(state == 1030){
            [data setState:Progress2];
        }else if(state ==1050){
            [data setState:Progress3];
        }else if(state == 2010){
            [data setState:Progress4];
        }else if (state == 3010){
            [data setState:Progress5];
        }else if (state == 3020){
            [data setState:Progress6];
        }else if(state == 9000){
            [data setState:Progress7];
        }else if(state == 9020){
            [data setState:Progress8];
        }else if(state == 9030){
            [data setState:Progress9];
        }else{
            [data setState:Progress1];
        }
        
        [array addObject:data];
    }
    
    
    if (array.count>0) {
        pageIndex++;
    }
    
    mod.dataMod = array;
    return mod;
}



-(void)refreshDetailData:(NSString*)dataId withCallBack:(FinishCallBack)callBack{
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        
        NSString *url = [API urlMake:OrderDetail];
        
        NSDictionary *dict = @{@"code":dataId};

        
        [Connect doPostWithCaCheWithUrl:url withFormdata:dict withCallBack:^(ResulteMod *resultMod) {
            
            
            //返回格式不同 需要单独处理
            NSString *code = [NSString stringWithFormat:@"%@",resultMod.resultDic[@"code"]];
            if ([StringTool isBlankString:code]) {
                resultMod.resultCode = @"";
            }else{
                resultMod.resultCode = code;
            }
            
            resultMod.resultTip = resultMod.resultDic[@"message"];
            
            if (![resultMod.resultCode isEqualToString:@"20003"]){
                resultMod.resltState = ResultFail;
            }else{
                
                OrderDetailData *data = [[OrderDetailData alloc]init];
                NSDictionary *dic = resultMod.resultDic[@"detailDto"];
                
                NSDictionary *orderdDic = dic[@"orderDto"];
                
                NSInteger state = 0;
                NSObject *stateObj = orderdDic[@"masterState"];
                if (![stateObj isKindOfClass:[NSNull class]]) {
                    state = [orderdDic[@"masterState"] integerValue];
                }
                
                if(state == 1010){
                    [data setState:Progress1];
                }else if(state == 1030){
                    [data setState:Progress2];
                }else if(state ==1050){
                    [data setState:Progress3];
                }else if(state == 2010){
                    [data setState:Progress4];
                }else if (state == 3010){
                    [data setState:Progress5];
                }else if (state == 3020){
                    [data setState:Progress6];
                }else if(state == 9000){
                    [data setState:Progress7];
                }else if(state == 9020){
                    [data setState:Progress8];
                }else if(state == 9030){
                    [data setState:Progress9];
                }else{
                    [data setState:Progress1];
                }
                
                
                [data setOrderCode:orderdDic[@"orderCode"]];
                
                long long ddddd = [orderdDic[@"createTime"] longLongValue];;
                [data setTime:[Tools llmilToDay:ddddd]];
                
                [data setTitle:orderdDic[@"housingName"]];
                [data setImgPath:[API imgUrlMake:orderdDic[@"thumb"]]];
                [data setDol:orderdDic[@"amountUsd"]];
                [data setRmb:orderdDic[@"amountCny"]];
                
                NSString *country = orderdDic[@"countryName"];
                NSString *city = orderdDic[@"cityName"];
                NSString *zhou = orderdDic[@"stateName"];
                
                NSString *str = [NSString stringWithFormat:@"%@-%@-%@"
                                 ,country,city,zhou];
                [data setSubleTitle:str];
                
                
                [data setBuyerName:dic[@"buyerName"]];
                [data setBuyerMobile:dic[@"buyerMobile"]];
                [data setBuyerImg:[API imgUrlMake:dic[@"buyerAvater"]]];
                [data setIntentionPrice:@"1600"];
          
                
                str = dic[@"sellerName"];
                if ([StringTool isBlankString:str]){
                    str = @"无数据";
                }
                [data setSalerName:str];
                
                
                str = dic[@"sellerMobile"];
                if ([StringTool isBlankString:str]){
                    str = @"无数据";
                }
                [data setSalerMobile:str];
                
                str = dic[@"sellerAvater"];
                if ([StringTool isBlankString:str]){
                    str = @"";
                }else{
                    str = [API imgUrlMake:str];
                }
                [data setSalerImg:str];
                
                
                str = dic[@"brokerageBudget"];
                if ([StringTool isBlankString:str]) {
                    str = @"无数据";
                }
                [data setMoneyRent:str];
                data.buyerAddress = dic[@"buyerAddress"];
                data.sellerAddress = dic[@"sellerAddress"];
                
                
                NSMutableArray *itemSectionArray = [[NSMutableArray alloc]init];
                NSArray *listArray = dic[@"orderFeeDtos"];
                for (int i =0;i<listArray.count;i+=4) {
                    
                    NSMutableArray *itemArray = [[NSMutableArray alloc]init];
                    
                    for (int j=i; j<i+4; j++) {
                        if (j<listArray.count) {
                            NSDictionary *itemD = [listArray objectAtIndex:j];
                            NearbyCellData *item = [NearbyCellData alloc];
                            [item setTitle:itemD[@"name"]];
                            [item setDetail:[NSString stringWithFormat:@"$%@",itemD[@"price"]]];
                            [itemArray addObject:item];
                        }
                    }
                     [itemSectionArray addObject:itemArray];
                }
                
                
                data.itemArray = itemSectionArray;
                resultMod.dataMod = data;
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                
                
                callBack(resultMod);
                
            });
        }];
    });
}


@end
